Dynomotion

Group: DynoMotion Message: 6409 From: himykabibble Date: 1/10/2013
Subject: KM_Controller CheckConnected()
I'm doing some long-overdue cleanup on my controller app, and I've run into a problem. I never got to the point of gracefully handling plugging and unplugging the board, as the status from KMController always seemed unreliable with v4.29z, which I've been using forever.

So, today I installed 4.30j, and started debugging. But, I still find CheckConnected() to be unreliable. With no board connected, CheckConnected() returns true! Is there some pre-requisite for this function to work correctly? The KM_Controller object has been created, and is, otherwise, working correctly. If I connect a board before starting my app, everything works fine, until I unplug it.

Regards,
Ray L.
Group: DynoMotion Message: 6410 From: Tom Kerekes Date: 1/10/2013
Subject: Re: KM_Controller CheckConnected()
Hi Ray,

CheckConnected may be poorly named.  It does not check if we are currently connected. It basically returns whether we encountered an error and became disconnected.  So after unplugging the board one error must occur before CheckConnected will inform you that the board is no longer connected. 

If you really think it through there is no protection in having a "Check if currently connected" query because immediately after you made the call the status might change and your application might crash.  This was all discussed previously here: 

http://tech.groups.yahoo.com/group/DynoMotion/message/6056

The updated examples in KMotion430j now demonstrate a 100% reliable method of handling this.  See the Dynomotion VB.NET, SimpleCSForms, and TeachMotion examples which is also the same method used in KMotionCNC.

There is basically no getting around writing your application in such a way as to expect and properly handle an error to occur at any time.  In earlier versions there were problems with errors not being properly thrown and returned or worse crashing internally.  But now we believe everything to be handled properly in the libraries.  For you it is actually quite easy.  Just put a Try/Catch around around all your block of status reads.  All your code can be written as if everything will succeed but if a disconnect occurs anywhere there will be an abort out to the Catch.

There is a method for checking if a board should be available for access.  It is:

            if (KM.WaitToken(100) == KMOTION_TOKEN.KMOTION_LOCKED)

This routine is optimized to return quickly in the common cases.  

The most common case is that the board is already opened and connected, no errors have occurred, and no other application is using the board.  In this case WaitToken exclusively locks the board and immediately returns KMOTION_LOCKED.

If the board has never been connected (or was previously disconnected due to an error) a new board will be check to be present and an attempt to open the board will be made.  If successful then KMOTION_LOCKED will be returned, otherwise KMOTION_NOT_CONNECTED.

If the board is already open and connected with no errors, and another application (or thread) has the board locked, then a wait for up to 100ms will occur to wait for the other applications to release the board.  If the board becomes available within the 100ms KMOTION_LOCKED will be returned.  If 100ms expires without gaining access KMOTION_IN_USE will be returned.

Note if WaitToken returns KMOTION_LOCKED it is your responsibility to ReleaseToken() ASAP.


An example of how to check if a board is available and also handle random disconnects the code fragment from SimpleFormsCS.cs

            if (KM.WaitToken(100) == KMOTION_TOKEN.KMOTION_LOCKED)
            {
                KM_MainStatus MainStatus;

                try
                {
                    MainStatus = KM.GetStatus(false);  // we already have a lock
                    KM.ReleaseToken();
                    XPos.Text = String.Format("{0:F1}", MainStatus.Destination[0]);
                    XEnabled.Checked = (MainStatus.Enables & (1<<0)) != 0;
                }
                catch (DMException ex) // in case disconnect in the middle of reading status
                {
                    KM.ReleaseToken();
                    MessageBox.Show(ex.InnerException.Message);
                }
            }

HTH
Regards
TK










Group: DynoMotion Message: 6411 From: himykabibble Date: 1/10/2013
Subject: Re: KM_Controller CheckConnected()
Tom,

This will be a HUGE help! Thanks!

Regards,
Ray L.

--- In DynoMotion@yahoogroups.com, Tom Kerekes wrote:
>
> Hi Ray,
>
> CheckConnected may be poorly named.  It does not check if we are currently connected. It basically returns whether we encountered an error and became disconnected.  So after unplugging the board one error must occur before CheckConnected will inform you that the board is no longer connected. 
>
> If you really think it through there is no protection in having a "Check if currently connected" query because immediately after you made the call the status might change and your application might crash.  This was all discussed previously here: 
>
> http://tech.groups.yahoo.com/group/DynoMotion/message/6056
>
> The updated examples in KMotion430j now demonstrate a 100% reliable method of handling this.  See the Dynomotion VB.NET, SimpleCSForms, and TeachMotion examples which is also the same method used in KMotionCNC.
>
> There is basically no getting around writing your application in such a way as to expect and properly handle an error to occur at any time.  In earlier versions there were problems with errors not being properly thrown and returned or worse crashing internally.  But now we believe everything to be handled properly in the libraries.  For you it is actually quite easy.  Just put a Try/Catch around around all your block of status reads.  All your code can be written as if everything will succeed but if a disconnect occurs anywhere there will be an abort out to the Catch.
>
> There is a method for checking if a board should be available for access.  It is:
>
>             if (KM.WaitToken(100) == KMOTION_TOKEN.KMOTION_LOCKED)
>
> This routine is optimized to return quickly in the common cases.  
>
> The most common case is that the board is already opened and connected, no errors have occurred, and no other application is using the board.  In this case WaitToken exclusively locks the board and immediately returns KMOTION_LOCKED.
>
> If the board has never been connected (or was previously disconnected due to an error) a new board will be check to be present and an attempt to open the board will be made.  If successful then KMOTION_LOCKED will be returned, otherwise KMOTION_NOT_CONNECTED.
>
> If the board is already open and connected with no errors, and another application (or thread) has the board locked, then a wait for up to 100ms will occur to wait for the other applications to release the board.  If the board becomes available within the 100ms KMOTION_LOCKED will be returned.  If 100ms expires without gaining access KMOTION_IN_USE will be returned.
>
> Note if WaitToken returns KMOTION_LOCKED it is your responsibility to ReleaseToken() ASAP.
>
>
> An example of how to check if a board is available and also handle random disconnects the code fragment from SimpleFormsCS.cs
>
>             if (KM.WaitToken(100) == KMOTION_TOKEN.KMOTION_LOCKED)
>             {
>                 KM_MainStatus MainStatus;
>
>                 try
>                 {
>                     MainStatus = KM.GetStatus(false);  // we already have a lock
>                     KM.ReleaseToken();
>                     XPos.Text = String.Format("{0:F1}", MainStatus.Destination[0]);
>                     XEnabled.Checked = (MainStatus.Enables & (1<<0)) != 0;
>                 }
>                 catch (DMException ex) // in case disconnect in the middle of reading status
>                 {
>                     KM.ReleaseToken();
>                     MessageBox.Show(ex.InnerException.Message);
>                 }
>             }
>
> HTH
> Regards
> TK
>
>
>
>
>
>
>
>
>
>
>
>
> ________________________________
> From: himykabibble
> To: DynoMotion@yahoogroups.com
> Sent: Thursday, January 10, 2013 3:56 PM
> Subject: [DynoMotion] KM_Controller CheckConnected()
>
>
>  
> I'm doing some long-overdue cleanup on my controller app, and I've run into a problem. I never got to the point of gracefully handling plugging and unplugging the board, as the status from KMController always seemed unreliable with v4.29z, which I've been using forever.
>
> So, today I installed 4.30j, and started debugging. But, I still find CheckConnected() to be unreliable. With no board connected, CheckConnected() returns true! Is there some pre-requisite for this function to work correctly? The KM_Controller object has been created, and is, otherwise, working correctly. If I connect a board before starting my app, everything works fine, until I unplug it.
>
> Regards,
> Ray L.
>